/*
//  Copyright © 2014 Allen County Public Library.

  //        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
 * Author: James Harpe
 * 
 * 
 * Description: This activity displays the list of saved users stored in the preferences file. It initiates the login
 * 				sequence when one of these users is selected. 
 */

package us.in.lib.acpl.Mobile_ACPL;

import java.util.ArrayList;

import com.google.analytics.tracking.android.EasyTracker;

import us.in.lib.acpl.Mobile_ACPL.adapters.SavedUsersAdapter;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import us.in.lib.acpl.Mobile_ACPL.patron.SavedUser;
import us.in.lib.acpl.Mobile_ACPL.util.JSONArrayPrefs;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;

public class SavedUserActivity extends ListActivity implements
		OnItemClickListener, OnClickListener {
	private ArrayList<SavedUser> savedUsers;
	private Activity thisActivity = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.saved_users);
		
		savedUsers = new ArrayList<SavedUser>();
		ArrayList<String> users = JSONArrayPrefs.getStringArrayPref(this,
				"savedUsers");
		ArrayList<String> pins = JSONArrayPrefs.getStringArrayPref(this,
				"savedPins");
		ArrayList<String> nicknames = JSONArrayPrefs.getStringArrayPref(this,
				"savedNicknames");
		ArrayList<String> remembers = JSONArrayPrefs.getStringArrayPref(this,
				"savedRemembers");
		for (int i = 0; i < users.size(); i++) {
			savedUsers.add(new SavedUser(users.get(i), pins.get(i), nicknames
					.get(i), remembers.get(i)));
		}
		this.findViewById(R.id.savedUsersLabel).setVisibility(View.VISIBLE);
		this.findViewById(R.id.savedUsersLayout).setOnClickListener(this);
		if (savedUsers.size() == 0) {
			this.findViewById(R.id.noSavedUsers).setVisibility(View.VISIBLE);
		}
		this.setListAdapter(new SavedUsersAdapter(this, savedUsers));
		this.getListView().setOnItemClickListener(this);
		this.findViewById(R.id.savedUsersCancel).setOnClickListener(this);
	}
	  @Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance().activityStart(this); // Add this method.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance().activityStop(this); // Add this method.
	  }

	@Override
	public void onItemClick(AdapterView<?> viewParent, View view, int position,
			long id) {

		boolean remember = savedUsers.get(position).isRemember();
		final String pin = savedUsers.get(position).getPin();
		final Intent intent = new Intent();
		intent.putExtra("user", savedUsers.get(position).getUser());
		intent.putExtra("pin", savedUsers.get(position).getPin());
		intent.putExtra("nickname", savedUsers.get(position).getNickname());

		if (remember) {

			this.setResult(RESULT_OK, intent);
			finish();
		} else {

			AlertDialog.Builder alert = new AlertDialog.Builder(this);

			alert.setTitle("Verify PIN");
			final RelativeLayout layout = (RelativeLayout) thisActivity
					.getLayoutInflater().inflate(R.layout.verify_pin, null);
			alert.setView(layout);
			EditText et = (EditText) layout
					.findViewById(R.id.verifyPinBox);
			et.setRawInputType(Configuration.KEYBOARD_QWERTY);
			alert.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog,
								int whichButton) {
							EditText et = (EditText) layout
									.findViewById(R.id.verifyPinBox);
							String value = et.getText().toString();

							if (value.equalsIgnoreCase(pin)) {
								setResult(RESULT_OK, intent);
								finish();
							} else {
								Toast toast = Toast.makeText(thisActivity,
										"Incorrect PIN. Please reenter.",
										Toast.LENGTH_SHORT);
								toast.setGravity(Gravity.CENTER, 0, 0);
								toast.show();
							}
						}
					});

			alert.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog,
								int whichButton) {
							// Canceled.
							dialog.dismiss();
						}
					});

			alert.show();

		}

	}

	@Override
	public void onClick(View arg0) {

		if (arg0.getId() != R.id.savedUsersLabel) {
			finish();
		}
	}


}
